package com.app.controller.miniprogram;

import com.app.common.Result;
import com.app.model.UserModel;
import com.app.service.MyService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
@RestController
@RequestMapping("/api/my")
public class MyController {
    @Autowired
    private UserService userService;
    @Autowired
    private MyService myService;

    //我的报名
    @GetMapping("/Application")
    public Result<Object> selectMyApplication(String name,Integer page, Integer limit,Integer status, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(myService.selectMyApplication(user.getId(),page,limit,status,name));
    }

    //我的积分
    @GetMapping("/Integral")
    public Result<Object> selectMyIntegral(Integer page, Integer limit,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(myService.selectMyIntegral(user.getId(),page,limit));
    }
}
